package net.dgg.dggcloud.service.jrcrm.adviser.product;

import net.dgg.dggcloud.core.minidao.hibernate.dao.IGenericBaseCommonDao;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerRecordConstant;
import net.dgg.dggcloud.service.jrcrm.entity.pro.Product;
import net.dgg.dggcloud.service.jrcrm.entity.pro.ProductAreaPrice;
import net.dgg.dggcloud.service.jrcrm.entity.pro.ProductCondition;
import net.dgg.dggcloud.service.jrcrm.service.cms.CustomerRecordService;
import net.dgg.dggcloud.service.jrcrm.utils.ObjCompareUtil;
import net.dgg.dggcloud.service.jrcrm.vo.ProductVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.Table;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 废弃
 * 原因：产品每次的修改都是将原来的老的产品标记为修改，修改后实际上是新增了一个产品，可以根据产品的
 * SerialNo查出所有历史记录。
 *
 * Created by litao on 2017/11/23.
 */
@Service
@Aspect
public class PorductRecordService {
    @Autowired
    CustomerRecordService customerRecordService;

    @Resource(name = "miniDaoHiberCommonDao")
    protected IGenericBaseCommonDao repository;

    //修改前的产品
    private ProductVo beforVo;
    //修改后的产品
    private ProductVo afterVo;

    /**
     * 新增的切点
     */
//    @Pointcut("execution(* net.dgg.dggcloud.service.jrcrm.service.pro.ProductService.saveOneDetailProduct(..))")
    public void addAspect(){}
    /**
     * 修改的切点
     */
//    @Pointcut("execution(* net.dgg.dggcloud.service.jrcrm.service.pro.ProductService.updateOneDetailProduct(..))")
    public void editAspect(){}
    /**
     * 删除的切点
     */
//    @Pointcut("execution(* net.dgg.dggcloud.service.jrcrm.service.pro.ProductService.batchDeleteByIds(..))")
    public void deleteAspect(){}


    /**
     * 新增完成后执行
     * @param jp
     */
//    @AfterReturning("addAspect()")
    public void addHistory(JoinPoint jp) {
        Object[] args = jp.getArgs();
        ProductVo vo = (ProductVo)args[0];
        Boolean isPush = (Boolean)args[1];
        User operatorUser = (User)args[2];


        Table tableAnnotation = Product.class.getAnnotation(Table.class);
        String tableName = tableAnnotation.name();
        Map record = new HashMap<>();
        record.put("cusId",vo.getProduct().getId());
        record.put("tableName",tableName);
        record.put("tableId",vo.getProduct().getId());
        record.put("recordType", CustomerRecordConstant.PRO_OPERATOR_ADD);
        record.put("operateUser", operatorUser.getId());
        record.put("ext3",vo.getProduct().getSerialNo());
        record.put("content", "新增了一个产品");

        customerRecordService.saveCustomerRecord(record);
    }


    /**
     * 修改完成前执行
     * @param jp
     */
//    @Before("editAspect()")
    public void editBefore(JoinPoint jp) throws CloneNotSupportedException {
        Object[] args = jp.getArgs();
        beforVo = new ProductVo();
        /**  查询数据库的老产品 **/
        Product product = repository.get(Product.class,((ProductVo)args[0]).getProduct().getId());
        beforVo.setProduct(product);

        Query areaQuery = repository.getSession().createQuery("From ProductAreaPrice where productId = ?");
        areaQuery.setParameter(0,((ProductVo)args[0]).getProduct().getId());
        List<ProductAreaPrice> productAreaPrices = areaQuery.list();
        beforVo.setProductAreaPrices(productAreaPrices);

        Query conditionQuery = repository.getSession().createQuery("From ProductCondition where productId = ?");
        conditionQuery.setParameter(0,((ProductVo)args[0]).getProduct().getId());
        List<ProductCondition> productConditions = conditionQuery.list();
        beforVo.setProductConditions(productConditions);
    }
    /**
     * 修改完成后执行
     * @param jp
     */
//    @AfterReturning("editAspect()")
    public void editHistory(JoinPoint jp) throws IllegalAccessException, ClassNotFoundException {
        Object[] args = jp.getArgs();
        ProductVo vo = (ProductVo)args[0];
        User operatorUser = (User)args[1];

        Table tableAnnotation = Product.class.getAnnotation(Table.class);
        String tableName = tableAnnotation.name();
        Map record = new HashMap<>();
        record.put("cusId",vo.getProduct().getId());
        record.put("tableName",tableName);
        record.put("tableId",vo.getProduct().getId());
        record.put("recordType", CustomerRecordConstant.PRO_OPERATOR_EDIT);
        record.put("operateUser", operatorUser.getId());
        record.put("ext3",vo.getProduct().getSerialNo());
        record.put("content", "修改了一个产品");

        customerRecordService.saveCustomerRecord(record);
    }


    /**
     * 删除完成后执行
     * @param jp
     */
//    @AfterReturning("deleteAspect()")
    public void delHistory(JoinPoint jp) {
        Object[] args = jp.getArgs();
        Long[] ids = (Long[])args[0];
        User operatorUser = (User)args[1];

        Table tableAnnotation = Product.class.getAnnotation(Table.class);
        String tableName = tableAnnotation.name();
        for(Long id : ids){

            Map record = new HashMap<>();
            record.put("cusId",id);
            record.put("tableName",tableName);
            record.put("tableId",id);
            record.put("recordType", CustomerRecordConstant.PRO_OPERATOR_DEL);
            record.put("operateUser", operatorUser.getId());
            record.put("content", "删除了一个产品（修改状态为4）");

            customerRecordService.saveCustomerRecord(record);
        }
    }
}
